package middle;

public class Solution_82 {

    public ListNode deleteDuplicates(ListNode head) {
        ListNode res = null, node = null, pre = null;
        boolean preFlag = true;
        while (head != null || (preFlag && pre != null)) {
            if (pre == null) {
                pre = head;
                preFlag = true;
            } else {
                if (head != null && pre.val == head.val) {
                    preFlag = false;
                } else {
                    if (preFlag) {
                        if (node != null) {
                            node.next = pre;
                        }
                        node = pre;
                        node.next = null;
                        if (res == null) {
                            res = node;
                        }
                    }
                    pre = head;
                    preFlag = true;
                }
            }
            if (head != null) {
                head = head.next;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution_82 model = new Solution_82();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(2);
//        head.next.next.next = new ListNode(2);
//        head.next.next.next.next = new ListNode(3);
//        head.next.next.next.next.next = new ListNode(4);
//        head.next.next.next.next.next.next = new ListNode(5);
        ListNode node = model.deleteDuplicates(head);
        System.out.println();
    }

    static class ListNode {

        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
